<template>
	<view class="">
		<u-datetime-picker :show="show" v-model="value1" mode="datetime" @confirm="confirm" cancelColor="#1BA95B"
			confirmColor="#1BA95B" @cancel="cancel"></u-datetime-picker>
		<view class="" style="padding-bottom: 120rpx;">
			<view class="mainpadding ffffff">
				<view class="ershiba nofonweight xiaohei">活动主图</view>
				<view class="margin_top shangc" v-if="!image"  @click="uploadImg(1)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
				<view class="margin_top shangc" v-if="image" @click="uploadImg(1)">
					<image :src="image_text" mode=""></image>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">活动轮播图（{{images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu" @click="delindex(index,images_text,images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="images.length<5" @click="uploadImg(5,images,images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view>
				<view class="margin_top titletext bold hei_text">活动视频</view>
				<view class="upzhizhao margin_top flexcenter" v-if="!hdjs_video" @click="upvideo()">
					<u-icon name="plus" color="#999" size="28"></u-icon>

				</view>
				<view v-if="hdjs_video" class="upzhizhao margin_top" style="position: relative;">
					<video :src="hdjs_video_text" object-fit="cover" class="upzhizhao"></video>
					<view class="shanchu" @click="delvideo">
						<u-icon name="close-circle-fill"></u-icon>
					</view>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">活动名称</view>
				<view class="margin_top">
					<u--input placeholder="请输入活动名称" border="surround" v-model="name"></u--input>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">活动详情图（{{hdjs_images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in hdjs_images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu"  @click="delindex(index,hdjs_images_text,hdjs_images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="hdjs_images.length<5" @click="uploadImg(5,hdjs_images,hdjs_images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="mainpadding ffffff margin_top1">
				<!-- <view class="xiahuaxian flexbetween">
					<view class="ershiba nofonweight xiaohei">组织人头像</view>
					<view class="touxiang">
						<image src="../static/image/system/ms.png" mode=""></image>
					</view>
				</view> -->
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">组织人姓名</view>
					<input class="textright" type="text" placeholder="输入姓名" v-model="zbfzzr">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">组织人手机号</view>
					<input class="textright" type="text" placeholder="输入组织人手机号" v-model="zbfmobile">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">报名开始时间</view>
					<view class="flexright" @click="messageTime.timeShow = true;timevalue=1">
						<input class="textright" type="text" placeholder="请选择" disabled v-model="bm_starttime">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">报名结束时间</view>
					<view class="flexright" @click="messageTime.timeShow = true;timevalue=2">
						<input class="textright" type="text" placeholder="请选择" disabled v-model="bm_endtime">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">活动开始时间</view>
					<view class="flexright" @click="messageTime.timeShow = true;timevalue=3">
						<input class="textright" type="text" placeholder="请选择" disabled v-model="bs_starttime">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">活动结束时间</view>
					<view class="flexright" @click="messageTime.timeShow = true;timevalue=4">
						<input class="textright" type="text" placeholder="请选择" disabled="" v-model="bs_endtime">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>

				<uni-data-picker @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区"
					v-model="area_id" class="zdy">
					<view class="xiahuaxian margin_top flexbetween">
						<view class="ershiba xiaohei nofonweight">城市</view>
						<view class="flexright">
							<input type="text" class="textright" placeholder="请选择" v-model="city_text" disabled=""
								style="width: 80%;">
							<u-icon name="arrow-right"></u-icon>
						</view>
					</view>
				</uni-data-picker>
				<view class="xiahuaxian flexbetween margin_top"  @click="chooselocation">
					<view class="ershiba nofonweight xiaohei">活动地址</view>
					<view class="flexright">
						<input type="text" class="textright" placeholder="请选择" v-model="address" disabled=""
							style="width: 80%;">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">活动类型</view>
					<view class="flexright">
						<u-radio-group v-model="type" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="个人活动"
								name="1"></u-radio>
							<u-radio activeColor="#1ba95b" label="团体活动" name="2"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top"  @click="xmShow = true">
					<view class="ershiba nofonweight xiaohei">项目</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="请选择" disabled="" v-model="cg_category_name">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">活动费用</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="输入活动费用" v-model="price">
						<view class="ershiba nofonweight xiaohei">元</view>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">参加{{type==1?'人数':'团队数'}}</view>
					<input class="textright" type="number" placeholder="输入报名人数" v-model="join_num">
				</view>
				<view class="xiahuaxian flexbetween margin_top" v-if="type==2">
					<view class="ershiba nofonweight xiaohei">参加团队人数</view>
					<input class="textright" type="number" placeholder="输入报名人数" v-model="team_num">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">男性人数</view>
					<input class="textright" type="number" placeholder="输入报名人数" v-model="man_num">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">女性人数</view>
					<input class="textright" type="number" placeholder="输入报名人数" v-model="womam_num">
				</view>
				<view class="xiahuaxian flexbetween margin_top" v-if="type==1">
					<view class="ershiba nofonweight xiaohei">性别</view>
					<view class="flexright">
						<u-radio-group v-model="xb_type" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="不限"
								name="0"></u-radio>
							<u-radio activeColor="#1ba95b" label="男" :customStyle="{marginRight: '30rpx'}"
								name="1"></u-radio>
							<u-radio activeColor="#1ba95b" label="女" name="2"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" v-if="listid">
					<view class="ershiba nofonweight xiaohei">是否上架</view>
					<view class="flexright">
						<u-radio-group v-model="status" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="上架"
								name="normal"></u-radio>
							<u-radio activeColor="#1ba95b" label="下架" name="hidden"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">活动介绍</view>
				<view class="margin_top">
					<u--textarea v-model="hdjs_des" placeholder="请输入活动介绍" count></u--textarea>
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">保存</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<u-picker :show="xmShow" :columns="xmList" title="选择类型" @cancel="xmShow=false" @confirm="xmconfirm"
			keyName="name"></u-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				value: '',
				value2: '',
				show: false,
				value1: Number(new Date()),
				image: "",
				image_text: "",
				images: [],
				images_text: [],
				name: "",
				zbfzzr: "",
				zbfmobile: "",
				bm_starttime: "",
				bm_endtime: "",
				bs_starttime: "",
				bs_endtime: "",
				hdjs_video: "",
				hdjs_video_text: "",
				hdjs_images: [],
				hdjs_images_text: [],
				hdjs_des: "",
				type: "1", //类型:1=个人活动2=团体活动
				province_id: "",
				city_id: "",
				area_id: "",
				// #ifdef H5
				latitude: "34.811007",
				longitude: "113.503393",
				address: "河南省郑州市中原区沟赵办事处水牛张村水云苑小区1号楼一单元",
				// #endif
				// #ifdef MP-WEIXIN || APP-PLUS
				latitude: "",
				longitude: "",
				address: "",
				// #endif
				price: "",
				join_num: "",
				team_num: "",
				man_num: "",
				womam_num: "",
				xb_type: "0", //性别类型:0=不限制,1=男性,2=女性
				cg_category_id: "", //项目
				cg_category_name:"",
				items: citys.data,
				city_text: "",
				upvalue:0,
				messageTime:{
					timeShow:false,
					title:"选择时间",
					mode:"datetime"//date日期，datetime年月日时分秒，time时间
				},
				timevalue:0,//1报名开始,2报名结束,3活动开始,4活动结束
				xmShow:false,
				xmList:[],
				status:"normal",//状态:normal=启用,hidden=隐藏、
				listid:"",
				user_zbfapply_listid:""
				
			}
		},
		onLoad(options) {
			this.user_zbfapply_listid=options.user_zbfapply_listid
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑活动"
				})
				this.listid = options.id
				this.getDetail()
			}
			this.getcgfl() //场馆分类/项目
		},
		methods: {
			getDetail(){
				httpRequest.request('/api/Activitycl/myActivityDetail', 'GET', {
					id:this.listid,
					user_zbfapply_id:this.user_zbfapply_listid
				}).then(res => {
					let data = res.data
					this.image=data.image
					this.image_text=data.image_text
					this.images=data.images.split(",")
					this.images_text=data.images_text
					this.hdjs_images=data.hdjs_images.split(",")
					this.hdjs_images_text=data.hdjs_images_text
					this.name=data.name
					this.zbfzzr=data.zbfzzr
					this.zbfmobile=data.zbfmobile
					this.bm_starttime=data.bm_starttime_text
					this.bm_endtime=data.bm_endtime_text
					this.bs_starttime=data.bs_starttime_text
					this.bs_endtime=data.bs_endtime_text
					this.hdjs_video=data.hdjs_video
					this.hdjs_des=data.hdjs_des
					this.type=data.type.toString()
					this.address=data.address
					this.longitude=data.longitude
					this.latitude=data.latitude
					this.price=data.price
					this.join_num=data.join_num
					this.team_num=data.team_num
					this.man_num=data.man_num
					this.xb_type=data.xb_type.toString()
					this.cg_category_id=data.cg_category_id
					this.cg_category_name=data.cgcategory.name
					this.womam_num=data.womam_num
					this.status=data.status
					this.city_text=data.cityinfo
					this.province_id=data.province_id
					this.city_id=data.city_id
					this.area_id=data.area_id
					this.address=data.address
				})
			},
			submit(){
				if(this.image==""){
					httpRequest.toast("请上传活动主图")
					return false
				}
				if(!this.images.length){
					httpRequest.toast("请上传活动轮播图")
					return false
				}
				if(this.name==""){
					httpRequest.toast("请输入活动名称")
					return false
				}
				if(this.zbfzzr==""){
					httpRequest.toast("请输入组织人姓名")
					return false
				}
				if(this.zbfmobile==""){
					httpRequest.toast("请输入组织人手机号")
					return false
				}
				if(this.bm_starttime==""){
					httpRequest.toast("请选择报名开始时间")
					return false
				}
				if(this.bm_starttime==""){
					httpRequest.toast("请选择报名结束时间")
					return false
				}
				if(this.bs_starttime==""){
					httpRequest.toast("请选择比赛开始时间")
					return false
				}
				if(this.bs_endtime==""){
					httpRequest.toast("请选择比赛结束时间")
					return false
				}
				if(this.area_id==""){
					httpRequest.toast("请选择城市")
					return false
				}
				if(this.address==""){
					httpRequest.toast("请选择活动地址")
					return false
				}
				if(this.cg_category_id==""){
					httpRequest.toast("请选择项目")
					return false
				}
				if(this.price==""){
					httpRequest.toast("请输入活动费用")
					return false
				}
				
				if(this.join_num==""){
					httpRequest.toast("请输入参加人数")
					return false
				}
				if(this.type==2){
					if(this.team_num==""){
						httpRequest.toast("请输入参加团队人数")
						return false
					}
					if(this.womam_num || this.man_num){
						if(this.womam_num!=this.team_num && this.man_num!=this.team_num){
							if (!this.man_num) {
								httpRequest.toast("请输入参加男性人数")
								return false
							}
							if (!this.womam_num) {
								httpRequest.toast("请输入参加女性人数")
								return false
							}
						}
						if(Number(this.womam_num)+Number(this.man_num) != Number(this.team_num)){
							httpRequest.toast("参加男性和女性人数总和要等于参加团队人数")
							return false
						}
					}
				}
				if(this.hdjs_des==""){
					httpRequest.toast("请输入活动介绍")
					return false
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let url = "/api/Activitycl/addActivity"
				let data = {
					image:this.image,
					images:this.images.join(),
					hdjs_images:this.hdjs_images.join(),
					name:this.name,
					zbfzzr:this.zbfzzr,
					zbfmobile:this.zbfmobile,
					bm_starttime:this.bm_starttime,
					bm_endtime:this.bm_endtime,
					bs_starttime:this.bs_starttime,
					bs_endtime:this.bs_endtime,
					hdjs_video:this.hdjs_video,
					hdjs_des:this.hdjs_des,
					type:this.type,
					address:this.address,
					longitude:this.longitude,
					latitude:this.latitude,
					price:this.price,
					join_num:this.join_num,
					team_num:this.team_num,
					man_num:this.man_num,
					xb_type:this.xb_type,
					cg_category_id:this.cg_category_id,
					womam_num:this.womam_num,
					province_id:this.province_id,
					city_id:this.city_id,
					area_id:this.area_id,
					address:this.address,
					user_zbfapply_id:this.user_zbfapply_listid
				}
				if(this.listid){
					url = "/api/Activitycl/editActivity"
					this.$set(data,"id",this.listid)
					this.$set(data,"status",this.status)
				}
				httpRequest.request(url, 'POST', data).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			xmconfirm(e){
				this.cg_category_id = e.value[0].id
				this.cg_category_name = e.value[0].name
				this.xmShow=false
			},
			getcgfl() {
				httpRequest.request('/api/index/cgCategory', 'POST', {}, false, false, true).then(res => {
					this.xmList.push(res.data)
				})
			},
			timeConfirm(e){
				if(this.timevalue ==1)this.bm_starttime = e
				if(this.timevalue ==2)this.bm_endtime = e
				if(this.timevalue ==3)this.bs_starttime = e
				if(this.timevalue ==4)this.bs_endtime = e
			},
			// 跳转完成创建
			tzwccj() {
				uni.navigateTo({
					url: '/pages_julebu/chuangjianwc'
				})
			},
			bindPickerChange(val) {
				let area = val.detail.value
				let area_text = [area[0].text, area[1].text, area[2].text];
				this.city_text = area_text.join("/")
				this.province_id = area[0].value;
				this.city_id = area[1].value;
				this.area_id = area[2].value;
			},
			confirm(e) {
				console.log(e)
				this.show = false
			},
			cancel() {
				this.show = false
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				this.upvalue = value
				let num = 0
				if(value==5){
					num = Number(value) - upImgs.length;
					if (num <= 0) {
						return false;
					}
				}else{
					num = 1
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			chooselocation(){
				let _this = this
				uni.chooseLocation({
					success: function(response) {
						console.log(response, "定位获取");
						_this.address = response.address
						_this.latitude = response.latitude
						_this.longitude = response.longitude
					},
					fail(res) {
						console.log(res, "失败了");
					}
				})
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if(this.upvalue==1){
						this.image = res.url
						this.image_text = image_item
						return false
					}
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upvideo() { //上传视频
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的视频
				uni.chooseVideo({
					maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageServicet(res.tempFilePath)
					}
				});
			},
			uploadImageServicet(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.hdjs_video = res.url
					this.hdjs_video_text = image_item
				}).catch(err => {});
			},
			delvideo(){
				this.hdjs_video = ""
				this.hdjs_video_text = ""
			}
		}
	}
</script>

<style lang="scss" scoped>
	.touxiang {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;

		image {
			border-radius: 50%;
			width: 100%;
			height: 100%;
		}
	}

	.dizneo {
		height: 500rpx;
		overflow: auto;
	}

	.xiaolbtn {
		width: 112rpx;
		height: 48rpx;
		background: rgba(27, 169, 91, 0.1);
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1BA95B;
		border: 2rpx solid #1BA95B;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {

		background: #F9F9F9;
	}

	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}

	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}

	.shilitu {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangc {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>